import json
import requests


class PictureAI:
    def __init__(self):
        # TODO: 需要改为ModelArts的token
        self.modelarts_token = "Token Of Modelarts"
        # TODO： 需要改为 ModelArts模型调用URL
        self.modelarts_url = "https://febb43f7f8c84dbea687de90f06fcf70.apigw.cn-north-1.huaweicloud.com/v1/infers/73f889df-f5a9-4b76-85e7-5020bccbaf22"
        self.header = {'x-auth-token': self.modelarts_token}

    def recognize_picture(self, file_path):
        files = {'images': (file_path, open(file_path, 'rb'), "image/png")}

        r = requests.post(self.modelarts_url, files=files, headers=self.header, verify=False)
        outputs = json.loads(r.content)
        return outputs


class RobotAI():
    def __init__(self):
        # 智能机器人URL，TODO： 改为 gallery的URL
        self.api_url = "https://gallery.cn-southwest-2.myhuaweicloud.com/v1/gallery/6e891e09-5228-4cab-bfcd-7b736f8871f8/"
        # TODO： 改为 gallery的app_code
        self.app_code = "app code"
        # 请求头
        self.header = {
            "Content-Type": "application/json",
            "X-Apig-AppCode": self.app_code
        }

    def get_anwswer(self, prompt):
        """
        从智能机器人获取回复

        :param prompt: str. 用户提问
        """
        payload = {
            "prompt": prompt,
            "max_tokens": 100,
            "top_k": -1,
            "top_p": 1,
            "temperature": 0,
            "ignore_eos": False,
            "stream": False
        }
        try:
            response = requests.post(self.api_url, headers=self.header, json=payload)
            # print(f"Response status code: {response.status_code}")
            # print(f"Response content: {response.text}")
            response.raise_for_status()

            text = response.json().get('text', [''])[0]
            return text
        except requests.exceptions.RequestException as e:
            print(f"请求错误{e}")
            return {"error": str(e)}
